
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "cursorio.h"
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

int fg_color[16] =
{
	0,
	FOREGROUND_RED,
	FOREGROUND_GREEN,
	FOREGROUND_BLUE,
	FOREGROUND_GREEN | FOREGROUND_BLUE,
	FOREGROUND_RED | FOREGROUND_BLUE,
	FOREGROUND_RED | FOREGROUND_GREEN,
	FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
	FOREGROUND_INTENSITY,
	FOREGROUND_INTENSITY | FOREGROUND_RED,
	FOREGROUND_INTENSITY | FOREGROUND_GREEN,
	FOREGROUND_INTENSITY | FOREGROUND_BLUE,
	FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
	FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
	FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
	FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
};

int bg_color[16] =
{
	0,
	BACKGROUND_RED,
	BACKGROUND_GREEN,
	BACKGROUND_BLUE,
	BACKGROUND_GREEN | BACKGROUND_BLUE,
	BACKGROUND_RED | BACKGROUND_BLUE,
	BACKGROUND_RED | BACKGROUND_GREEN,
	BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
	BACKGROUND_INTENSITY,
	BACKGROUND_INTENSITY | BACKGROUND_RED,
	BACKGROUND_INTENSITY | BACKGROUND_GREEN,
	BACKGROUND_INTENSITY | BACKGROUND_BLUE,
	BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE,
	BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE,
	BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN,
	BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
};

int __BACKGROUND = 0;
int __FOREGROUND = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;

/**********************************************************************/
/*	Name:     gotoxy()                                                */
/*																	  */
/*	Return:   Void                                                    */
/*                                                                    */
/*	Function: This function takes x and y chords and places the       */
/*            cursor on that possition on the window.                 */
/**********************************************************************/
void gotoxy(short x, short y)
{
	COORD pos = {x, y};
	SetConsoleCursorPosition(hConsole, pos);
	return;
}

/**********************************************************************/
/*	Name:     testcolor()                                             */
/*																	  */
/*	Return:   Void                                                    */
/*                                                                    */
/*	Function: This function takes a color id and changes att text     */
/*            outputed after this function to that color.             */
/**********************************************************************/
void textcolor(int color)
{
	if((color < 16) && (color >= 0))
	{
		__FOREGROUND = fg_color[color];
		SetConsoleTextAttribute(hConsole, __FOREGROUND | __BACKGROUND);
	}
	return;
}

/**********************************************************************/
/*	Name:     textbackground()                                        */
/*																	  */
/*	Return:   Void                                                    */
/*                                                                    */
/*	Function: This function takes a color ID and changes the          */
/*            background of everything outputed after this function   */
/*		      to that color.                                          */
/**********************************************************************/
void textbackground(int color)
{
	if((color < 16) && (color >= 0))
	{
		__BACKGROUND = bg_color[color];
		SetConsoleTextAttribute(hConsole, __FOREGROUND | __BACKGROUND);
	}
	return;
}

/**********************************************************************/
/*	Name:     clrscr()                                                */
/*																	  */
/*	Return:   Void                                                    */
/*                                                                    */
/*	Function: This function simply clears the screen of all text .    */
/**********************************************************************/
void clrscr()
{
	COORD coordScreen = {0, 0};
	DWORD cCharsWritten;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	DWORD dwConSize;
	GetConsoleScreenBufferInfo(hConsole, &csbi);
	dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
	FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
	GetConsoleScreenBufferInfo(hConsole, &csbi);
	FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
	SetConsoleCursorPosition(hConsole,coordScreen);
	return;
}

/**********************************************************************/
/*	Name:     center()                                                */
/*																	  */
/*	Return:   Void                                                    */
/*                                                                    */
/*	Function: This function takes a string and prints it to the center*/
/*            of the screen on the current line.                      */
/**********************************************************************/
void center( char buffer[] ) {
	for(int x = 0; x < ( 40 - ( strlen( buffer ) / 2 ) ); x++ ) {
		printf(" ");
	}
	printf("%s", buffer);
}
void centern( char buffer[] ) {
	for(int x = 0; x < ( 40 - ( strlen( buffer ) / 2 ) ); x++ ) {
		printf(" ");
	}
	printf("%s\n", buffer);
}
/**********************************************************************/
/*	Name:     dot()                                                   */
/*																	  */
/*	Return:   Void                                                    */
/*                                                                    */
/*	Function: This functon takes a two numbers and produces a dot     */
/*            waiting effect                                          */
/**********************************************************************/
void dot( int dots, int dur, char sym ) {
	for( int x = 0; x < dots; x++ ) {
		printf( "%c", sym );
		Sleep(dur);
	}
}
